package com.zxlfysj.linkedlist;

/**
 * 约瑟夫问题,使用环形链表实现
 * @author yangshujing
 * @create 2020-08-16 17:10
 */
public class Josepfu {
    public static void main(String[] args) {
        //测试
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        //创建小孩环形
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();
        //测试小孩出圈顺序
        circleSingleLinkedList.countBoy(1, 2, 5);
    }
}

//创建一个环形的单向链表
class CircleSingleLinkedList {
    //创建一个first节点，目前没有编号
    private Boy first = null;



    /**
     * 计算小孩出圈的顺序
     * @param startNo 表示从哪个小孩开始
     * @param countNum  表示报数几下出圈
     * @param nums  表示最初小孩总数
     */
    public void countBoy(int startNo, int countNum, int nums) {

        if(first == null || startNo < 1 || startNo > nums) {
            System.out.println("参数错误");
            return;
        }
        Boy helper = first;//辅助指针
        //让辅助指针移动到最后一个小孩
        while (true) {
            if(helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }
        //报数前，先让first和helper指针移动（startNo - 1）次
        for (int j = 0; j < startNo - 1; j++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //开始报数，循环出圈
        //报数时，让first和helper指针同时移动（countNum - 1）次，然后出圈
        while (true) {
            if(helper == first) { //说明圈中只有一个小孩了
                break;
            }

            for (int j = 0; j < countNum - 1; j++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            //这时first指向要出圈的小孩
            System.out.printf("小孩%d出圈\n", first.getNo());
            //出圈
            first = first.getNext();
            helper.setNext(first);

        }

        System.out.printf("最后留在圈中的小孩编号是%d", first.getNo());



    }

    /**
     * 添加小孩节点，构建环形链表
     * @param nums 表示总共几个小孩
     */
    public void addBoy(int nums) {
        if(nums < 1) {
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy = null;//辅助指针
        for(int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if(i == 1) {
                first = boy;
                first.setNext(first);//构成环
                curBoy = first;
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    //遍历
    public void showBoy() {
        if(first == null) {
            System.out.println("链表是空");
            return;
        }
        //first不能动，使用辅助指针
        Boy curBoy = first;
        while(true) {
            System.out.printf("小孩的编号%d \n", curBoy.getNo());
            if(curBoy.getNext() == first) { //已遍历到最后一个小孩
                break;
            }
            curBoy = curBoy.getNext();//指针后移
        }
    }
}

class Boy {
    private int no; //编号
    private Boy next; //指向下一个节点

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}
